package com.hcryan.djm.result;

import com.hcryan.djm.exception.ErrorMessage;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author ryanwdj
 * @since 2025/2/18
 */

public class WebResult<T> {

    @ApiModelProperty("处理结果")
    private int status = 0;
    @ApiModelProperty("返回数据")
    private T data;
    @ApiModelProperty("处理消息")
    private String message;

    private static final WebResult succeed = new WebResult(0, (Object)null, "成功");

    public static <T> WebResult<T> success() {
        return succeed;
    }

    public static <T> WebResult<T> success(T data) {
        return success(data, (String)null);
    }

    public static <T> WebResult<T> success(String message) {
        return (WebResult<T>) success((Object)null, message);
    }

    public static <T> WebResult<T> success(T data, String message) {
        return new WebResult(0, data, message);
    }

    public static <T> WebResult<T> failure(ErrorMessage errorMessage){
        return failure(errorMessage.getCode(), errorMessage.getMessage());
    }
    public static <T> WebResult<T> failure(ErrorMessage errorMessage, String customErrorMessage){
        return failure(errorMessage.getCode(), customErrorMessage);
    }

    public static <T> WebResult<T> failure(int status, String message) {
        return (WebResult<T>) failure(status, (Object)null, message);
    }

    public static <T> WebResult<T> failure(int status, T data, String message) {
        return new WebResult(status < 400 ? 400 : status, data, message);
    }

    public static <T> WebResult<T> exception(int status, String message) {
        return exception(status, (Object)null, message);
    }

    public static <T> WebResult<T> exception(int status, Object data, String message) {
        return new WebResult(status < 500 ? 500 : status, data, message);
    }

    public boolean isSuccess() {
        return this.status == 0;
    }

    public String toString() {
        return "WebResult{status=" + this.status + ", data=" + this.data + ", message='" + this.message + '\'' + '}';
    }

    public int getStatus() {
        return this.status;
    }

    public T getData() {
        return this.data;
    }

    public String getMessage() {
        return this.message;
    }

    public void setStatus(final int status) {
        this.status = status;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public WebResult() {
    }

    public WebResult(final int status, final T data, final String message) {
        this.status = status;
        this.data = data;
        this.message = message;
    }
}
